package com.blackline.UI.Logon;

import java.util.concurrent.TimeUnit;

import org.openqa.selenium.WebDriver;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.support.FindBy;
import org.openqa.selenium.support.PageFactory;
import org.apache.log4j.Logger;

import com.blackline.UI.SessionMenu.*;

/**
 * @author XBO
 * @version Jan, 2013
 * <p>This UI is the Logon page of Blackline system.
 * Please refer to UI.Logon.png in doc-files.
 */
public class logonPage {
	private static Logger logger=Logger.getLogger(logonPage.class);
    /**
     * Login ID text field
     */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_tbLoginID")
	public WebElement LoginID;
	
	/**
	 * Password text field
	 */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_tbPassword")
	public WebElement Password;
	
	/**
	 * 	Login button
	 */
	@FindBy(id = "ctl00_ctl00_contentBody_cphMain_btnSubmit")
	public WebElement LoginButton;
	
	/**
	 * Verify whether login successfully.
	 * @param role admin or other roles
	 * @return if return nothing (""), means success; otherwise return "Failed: Logon"
	 * and this information will be output in verificationErrors;
	 */
    public String VerifyLogon(String role,WebDriver driver) {
    	sessionMenu sessionMenu = PageFactory.initElements(driver, sessionMenu.class);
    	if(sessionMenu.LogOut.isDisplayed()){
    		logger.info("[Pass!] User '"+role + "' has logged in successfully.");
        	return "";
    	}else{
        	logger.info("Failed: "+role+" login failed");
        	return "[Failed!] User '"+role+"' failed to login.</b>";
        }   
    }
    
	/**
	 * The method to log into Blackline system.
	 * @param driver The webdriver to use
	 * @param site The site to logon
	 * @param username The user to logon
	 * @param password The user's password
	 * <p>Sample:<br>
	 * UI.LogonPage.Login(driver, parameters.urlDev3, parameters.getLogonID("admin"), parameters.getPassword("admin"));
	 */
    public void Login(WebDriver driver, String site, String username, String password)
    {
    	StringBuffer verificationErrors = new StringBuffer();
    	
        driver.manage().timeouts().implicitlyWait(30, TimeUnit.SECONDS);
        driver.manage().window().maximize();
        
        driver.get(site);
        logger.info("Going to log into: " + site);
        this.LoginID.clear();
        this.LoginID.sendKeys(username);
        logger.info("User: " + username);
        this.Password.clear();
        this.Password.sendKeys(password);
        logger.info("Password is typed in.");
        this.LoginButton.click();
        logger.info("Clicked Login button");
        verificationErrors.append(this.VerifyLogon(username, driver));
    }
    
    
} 